#!/bin/bash

# This file is part of libmarco
# http://code.google.com/p/libmarco/

# A wrapper for MacFuse ssh filesystem: sshfs-static-leopard
# Allows to create a list of favorite servers mounts
# Invoke the script to select from a numbered list
# (at the end of this script)

# MacFuse is required: http://code.google.com/p/macfuse/downloads
AUTOINST_BIN="/Library/Filesystems/fusefs.fs/Support/autoinstall-macfuse-core"
# SSHFS can be downloaded trough svn:
# svn co http://macfuse.googlecode.com/svn/trunk/filesystems/sshfs/binary ~/sshfs-binaries
SSHFS_BIN="$HOME/bin/sshfs-static-leopard"

#Default mount options, don't use 'volname' here!
SFM_MOUNT_OPTS="auto_cache,reconnect" 

# ======================================================================

# Ask wether to apply the autoinstall updates now
function ask_update () {
    local ANSW;
    echo;
    echo "Perform the update now? y/n [n]";
    read ANSW
    if [[ "$ANSW" == "y" ]]; then
        echo;
        echo "Please run this command as root:";
        echo "$AUTOINST_BIN -i";
        echo "press enter to continue";
        read;
    else
        echo "update skipped";
    fi
}

# Updates sshfs binary and checks for autoinstall updates
function check_for_updates () {
# Update sshfs bin from svn
cd `dirname $SSHFS_BIN`;
svn up
cd - &>/dev/null  

# Check for updates and offer the possibility
# to install it now
UPDATES=`$AUTOINST_BIN -l`
if [ "$UPDATES" == "" ]; then
    echo "No updates available"
else
    echo "Updates available!!!"
    echo "$UPDATES"
    ask_update 
fi
}

# Register a new server in the list
function register_setting () {
let 'REG_COUNT += 1';
# Name  User    Server  path    Vol_name    options
# $1    $2      $3      $4      $5          $6

# Print this setting
echo "$REG_COUNT ---> $1";
echo "   $2@$3:$4"
echo "   Mounts as \"$5\""

# Save info 
sfm_name[$REG_COUNT]="$1";
sfm_user[$REG_COUNT]="$2";
sfm_server[$REG_COUNT]="$3";
sfm_path[$REG_COUNT]="$4";
sfm_volname[$REG_COUNT]="$5";
sfm_options[$REG_COUNT]="$6";

local volumestring="volname=${sfm_volname[$REG_COUNT]}"
if [[ "$6" == "default" ]]; then
    sfm_options[$REG_COUNT]="$SFM_MOUNT_OPTS,$volumestring"
    echo "   Options: (default) ${sfm_options[$REG_COUNT]}"
elif [[ "$6" == "" ]]; then
    sfm_options[$REG_COUNT]="$volumestring"
    echo "   Options: (no options) ${sfm_options[$REG_COUNT]}"
else 
    sfm_options[$REG_COUNT]="$6,$volumestring"
    echo "   Options: (custom) ${sfm_options[$REG_COUNT]}"
fi
echo;

}

# Ask the user to select a numeric alternative and run it
function select_and_mount () {
let 'REG_COUNT += 1'
echo "$REG_COUNT ---> Check for updates";
echo

# Read setting id as integer
local i;
echo "_________________________________________________________________________"
echo "What you want mount? [1...$REG_COUNT]?";
read i;
let 'i += 0';


if [[ $i == $REG_COUNT ]]; then
    # Update!
    check_for_updates
    exit;

elif [[ $i -lt 1 || $i -gt $REG_COUNT ]]; then
    # Discard if out of bounds
    echo "Invalid setting: $i"
    echo "must be a number in [1...$REG_COUNT]"
    exit;
fi

echo "Mounting ${sfm_name[$i]}"

local mountdir="/tmp/${sfm_volname[$i]}";
umount $mountdir &> /dev/null; #If something else is mounted there, unmount
mkdir -p $mountdir; #Create mount dir

# Remote user@url:path
local fullpath="${sfm_user[$i]}@${sfm_server[$i]}:${sfm_path[$i]}";
# Mount options
local opt_string="-o ${sfm_options[$i]}";

local cmd="$SSHFS_BIN $fullpath $mountdir $opt_string";
echo;
echo "command is:"
echo "$cmd"
$cmd
}


REG_COUNT=0;
echo;
echo "Listing configurations:";
echo "_________________________________________________________________________"

# Settings go here:

# register_setting <name> <user> <host> <remote_path> <volume_name> <options_string> 
# <name> is the configuration name
# <user> is the remote username
# <host> is the remote hostname
# <remote_path> is the remote path to mount
# <volume_name> is the name to give to the mounted volume
# <options_string> is any string to pass to the -o option of the sshfs binary
# ! Note !
# <remote_path> must be a full path (i.e. "~" does not work)
# <volume_name> must be unique for each setting registered
# <options_string> can be "default" (see $SFM_MOUNT_OPTS) "" (no options), 
#  any other valid options string is fine, except for volname which is added later.
#  see: http://code.google.com/p/macfuse/wiki/OPTIONS

register_setting "Server1-home" "myuser" "my.server.com" "/home/myuser" "Srv1" "default"
register_setting "Server2-Web" "webadmin" "server2.com" "/web" "S2-Web" "auto_cache,reconnect,port=6666"


# Run after registering all settings 
select_and_mount
